//
//  WithdrawViewController.m
//  work
//
//  Created by 徐宏飞 on 16/6/1.
//  Copyright © 2016年 方伟. All rights reserved.
//

#import "WithdrawViewController.h"

@interface WithdrawViewController ()<NetworkToolerDelegate>

@property (weak, nonatomic) IBOutlet UIScrollView *scrollV;
@property (weak, nonatomic) IBOutlet UIView *baseView;
@property (nonatomic, strong) UITapGestureRecognizer *tap;
@property (strong, nonatomic)NetworkTooler *networkTool;




@property (weak, nonatomic) IBOutlet UILabel *lblWorkerName;
@property (weak, nonatomic) IBOutlet UITextField *tfdBank;
@property (weak, nonatomic) IBOutlet UITextField *tfdFirstBank;
@property (weak, nonatomic) IBOutlet UITextField *tfdBankCardNO;
@property (weak, nonatomic) IBOutlet UITextField *tfdWithdnum;





@end

@implementation WithdrawViewController


#pragma mark - life cycle
- (void)initNavigationBar
{
    self.enableReturnButton = YES;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
    self.title = @"提现";
    
    [_baseView addGestureRecognizer:self.tap];
    
    _networkTool = [NetworkTooler new];
    _lblWorkerName.text = _userName;
}

- (void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
    
    if (SCREEN_HEIGHT == 480) {
        self.scrollV.scrollEnabled = NO;
        self.scrollV.showsHorizontalScrollIndicator = NO;
    } else {
        self.scrollV.scrollEnabled = YES;
    }

    [MobClick beginLogPageView:@"Cash"];//("PageOne"为页面名称，可自定义)

}
- (void)viewWillDisappear:(BOOL)animated
{
    [super viewWillDisappear:animated];
    [MobClick endLogPageView:@"Cash"];
}
- (void)viewDidAppear:(BOOL)animated{
    [super viewDidAppear:animated];
    
    
    //监听键盘的弹出事件
    NSNotificationCenter *center = [NSNotificationCenter defaultCenter];
    
    [center addObserver:self selector:@selector(keyboardWillChangeFrame:) name:UIKeyboardWillChangeFrameNotification object:nil];
}

- (void)viewDidDisappear:(BOOL)animated{
    //移除监听
    [[NSNotificationCenter defaultCenter] removeObserver:self];
}






#pragma mark - UITableViewDelegate






#pragma mark - NetworkToolerDelegate
- (void)returnData:(id)data{
    
    
    
    [self actionAlert];
    
}
- (void)returnRequestError:(NSError *)error{


}
- (void)returnMsg:(NSString *)Msg{

    [self showAlertWithTitle:@"温馨提示" message:Msg cancelButtonTitle:@"取消" otherButtonTitles:@"确定"];
}








#pragma mark - event response
- (void)keyboardWillChangeFrame:(NSNotification *)notification{
    //获取键盘的Y值
    CGRect rect = [[notification.userInfo objectForKey:@"UIKeyboardFrameEndUserInfoKey"] CGRectValue];
    CGFloat keybordY = rect.origin.y;
    CGFloat tranformValue = keybordY - self.view.frame.size.height;
    
    CGFloat value = 0.0;
    if (SCREEN_HEIGHT == 667) {  //6s
        value = 0;
    }
    if (SCREEN_HEIGHT == 568) {  //5 5s
        value = 0.6;
    }
    
    if (SCREEN_HEIGHT == 480) {  // 4s
        value = 0.6;
    }
    
    if (tranformValue >= 0) {
        tranformValue = 0;
    }
    
    CGPoint point = CGPointMake(0, -tranformValue*value);
    _scrollV.contentOffset = point;
    
}

- (IBAction)btnWithdrawAction:(id)sender {
    //过滤
    if ([self.tfdBank.text isEqualToString:@""]) {
        [self showAlertWithTitle:@"温馨提示" message:@"提现银行不能为空" cancelButtonTitle:@"取消" otherButtonTitles:@"确定"];
        return;
    }
    if ([self.tfdFirstBank.text isEqualToString:@""]) {
        [self showAlertWithTitle:@"温馨提示" message:@"开户行不能为空" cancelButtonTitle:@"取消" otherButtonTitles:@"确定"];
        return;
    }
    if ([self.tfdBankCardNO.text isEqualToString:@""]) {
        [self showAlertWithTitle:@"温馨提示" message:@"银行卡号不能为空" cancelButtonTitle:@"取消" otherButtonTitles:@"确定"];
        return;
    }
    if (![self BankCardNumIsvalid:self.tfdBankCardNO.text]) {
        [self showAlertWithTitle:@"温馨提示" message:@"银行卡号格式错误" cancelButtonTitle:@"取消" otherButtonTitles:@"确定"];
        return;
    }
    
    
    
    if ([self.tfdWithdnum.text isEqualToString:@""]) {
        [self showAlertWithTitle:@"温馨提示" message:@"提现金额不能为空" cancelButtonTitle:@"取消" otherButtonTitles:@"确定"];
        return;
    }
    
    if (self.tfdWithdnum.text.integerValue < 100) {
        [self showAlertWithTitle:@"温馨提示" message:@"提现金额不能小于100元" cancelButtonTitle:@"取消" otherButtonTitles:@"确定"];
        return;
    }
    
    NSInteger minValue = _maxOutput.integerValue < 20000 ? _maxOutput.integerValue : 20000;
    if (self.tfdWithdnum.text.integerValue > minValue) {
        [self showAlertWithTitle:nil message:[NSString stringWithFormat:@"提现金额不能大于%ld元",minValue] cancelButtonTitle:@"取消" otherButtonTitles:@"确定"];
        return;
    }
    
    
    //申请
    [self _postWithdrawRequst];
}




- (void)TapAction{
    [self.view endEditing:YES];
}





#pragma mark - private methods
- (void) _postWithdrawRequst{
    //    self.requestType = MINEREQUESSTTYPEME;
    
    NSDictionary *parameters = @{
                                 @"session_id":_session_id,
                                 @"amount":self.tfdWithdnum.text,
                                 @"bank_name":self.tfdBank.text,
                                 @"open_bank":self.tfdFirstBank.text,
                                 @"bankcard_no":self.tfdBankCardNO.text
                                 };
    
    NSLog(@"%@", parameters);
    
    _networkTool.delegate = self;
    [_networkTool parameters:parameters withURL:getApplyWithdraw_show withIsPaparameters:YES];
    
    
}


- (void)actionAlert{
    [self.view endEditing:YES];
    UIAlertController *alertC = [UIAlertController alertControllerWithTitle:@"提示!" message:@"操作成功" preferredStyle:UIAlertControllerStyleAlert];
    
    [self presentViewController:alertC animated:YES completion:^{
    }];
    
    //添加一个确定按钮
    UIAlertAction *alertA = [UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
        
        [self.navigationController popViewControllerAnimated:YES];
        
    }];
    //添加提示按钮
    [alertC addAction:alertA];
}




#pragma mark - gettsers and setters
- (UITapGestureRecognizer *)tap{
    if (!_tap) {
        _tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(TapAction)];
    }
    
    return _tap;
}



- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
